<template>
  <div class="app-container">
    <div class="product-detail-container" v-loading="loading">
      <!-- 商品基础信息 -->
      <div class="product-basic-info" v-if="productDetail">
        <el-row :gutter="20">
          <!-- 商品图片区域 -->
          <el-col :span="12">
            <div class="product-images">
              <el-carousel v-if="productDetail.productImages && productDetail.productImages.length > 0" height="400px">
                <el-carousel-item v-for="(image, index) in productDetail.productImages" :key="index">
                  <img :src="image.imageUrl" :alt="productDetail.productSpu && productDetail.productSpu.productName" class="product-image" />
                </el-carousel-item>
              </el-carousel>
              <div v-else class="no-image">
                <i class="el-icon-picture-outline"></i>
                <p>暂无图片</p>
              </div>
            </div>
          </el-col>
          
          <!-- 商品信息区域 -->
          <el-col :span="12">
            <div class="product-info">
              <h1 class="product-name">{{ (productDetail.productSpu && productDetail.productSpu.productName) || '商品名称' }}</h1>
              
              <!-- 价格信息 -->
              <div class="price-section">
                <span class="current-price">¥{{ (productDetail.productSku && productDetail.productSku.productSellingPrice) || '0.00' }}</span>
                <span v-if="productDetail.productSku && productDetail.productSku.productPrice" class="original-price">¥{{ productDetail.productSku.productPrice }}</span>
                <span v-if="productDetail.discountText" class="discount-text">{{ productDetail.discountText }}</span>
              </div>
              
              <!-- 商品规格 -->
              <div class="specifications" v-if="productDetail.specifications && productDetail.specifications.length > 0">
                <h3>商品规格</h3>
                <div class="spec-list">
                  <div v-for="spec in productDetail.specifications" :key="spec.id" class="spec-item">
                    <span class="spec-name">{{ spec.specName }}：</span>
                    <span class="spec-value">{{ spec.specValue }}</span>
                  </div>
                </div>
              </div>
              
              <!-- 库存信息 -->
              <div class="stock-info">
                <span class="stock-label">库存：</span>
                <span :class="['stock-count', productDetail.inStock ? 'in-stock' : 'out-of-stock']">
                  {{ productDetail.stock || 0 }}件
                </span>
                <span v-if="productDetail.inStock" class="stock-status">现货</span>
                <span v-else class="stock-status out-of-stock">缺货</span>
              </div>
              
              <!-- 商家信息 -->
              <div class="merchant-info" v-if="productDetail.merchant">
                <h3>商家信息</h3>
                <div class="merchant-details">
                  <p><strong>商家名称：</strong>{{ productDetail.merchant.merchantName }}</p>
                  <p><strong>联系方式：</strong>{{ productDetail.merchant.contactPhone }}</p>
                  <p><strong>商家地址：</strong>{{ productDetail.merchant.merchantAddress || '暂无地址信息' }}</p>
                </div>
              </div>
              
              <!-- 操作按钮 -->
              <div class="action-buttons">
                <el-button type="primary" size="large" :disabled="!productDetail.inStock" @click="handleBuyNow">
                  立即购买
                </el-button>
                <el-button type="success" size="large" :disabled="!productDetail.inStock" @click="handleAddToCart">
                  加入购物车
                </el-button>
                <el-button type="info" size="large" @click="handleAddToFavorites">
                  收藏商品
                </el-button>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      
      <!-- 商品详情标签页 -->
      <div class="product-tabs" v-if="productDetail">
        <el-tabs v-model="activeTab" type="border-card">
          <!-- 商品详情 -->
          <el-tab-pane label="商品详情" name="detail">
            <div class="product-description">
              <h3>商品描述</h3>
              <p>{{ (productDetail.productSpu && productDetail.productSpu.productDescription) || '暂无商品描述' }}</p>
            </div>
          </el-tab-pane>
          
          <!-- 商品规格 -->
          <el-tab-pane label="规格参数" name="specs">
            <div class="specifications-detail" v-if="productDetail.specifications && productDetail.specifications.length > 0">
              <el-table :data="productDetail.specifications" border>
                <el-table-column prop="specName" label="规格名称" width="200"></el-table-column>
                <el-table-column prop="specValue" label="规格值"></el-table-column>
              </el-table>
            </div>
            <div v-else class="no-specs">
              <p>暂无规格参数</p>
            </div>
          </el-tab-pane>
          
          <!-- 商品评价 -->
          <el-tab-pane label="用户评价" name="reviews">
            <div class="reviews-section" v-if="productDetail.reviews && productDetail.reviews.length > 0">
              <div class="rating-overview">
                <div class="rating-score">
                  <span class="score">{{ (productDetail.averageRating && productDetail.averageRating.toFixed(1)) || '0.0' }}</span>
                  <el-rate v-model="productDetail.averageRating" disabled show-score></el-rate>
                </div>
                <div class="rating-stats">
                  <p>共{{ productDetail.reviewCount || 0 }}条评价</p>
                </div>
              </div>
              
              <div class="reviews-list">
                <div v-for="review in productDetail.reviews" :key="review.id" class="review-item">
                  <div class="review-header">
                    <div class="user-info">
                      <span class="user-name">{{ review.userName }}</span>
                      <el-rate v-model="review.rating" disabled size="small"></el-rate>
                    </div>
                    <span class="review-date">{{ formatDate(review.createTime) }}</span>
                  </div>
                  <div class="review-content">{{ review.content }}</div>
                </div>
              </div>
            </div>
            <div v-else class="no-reviews">
              <p>暂无用户评价</p>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      
      <!-- 推荐商品 -->
      <div class="recommend-products" v-if="productDetail.recommendProducts && productDetail.recommendProducts.length > 0">
        <h2>推荐商品</h2>
        <el-row :gutter="20">
          <el-col :span="6" v-for="product in productDetail.recommendProducts" :key="product.id">
            <div class="recommend-item" @click="goToProduct(product.id)">
              <img :src="product.productMainImage || '/static/images/no-image.png'" :alt="product.productName" class="recommend-image" />
              <h4>{{ product.productName }}</h4>
              <p class="recommend-price">¥{{ product.productPrice }}</p>
              <div class="recommend-actions">
                <el-button size="mini" type="primary">查看详情</el-button>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      
      <!-- 无数据状态 -->
      <div v-if="!loading && !productDetail" class="no-data">
        <i class="el-icon-warning"></i>
        <p>商品信息加载失败</p>
        <el-button type="primary" @click="loadProductDetail">重新加载</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getProductDetail } from "@/api/product";

export default {
  name: "ProductDetail",
  data() {
    return {
      loading: false,
      productDetail: null,
      skuId: null,
      activeTab: 'detail'
    };
  },
  created() {
    this.skuId = this.$route.params.skuId;
    if (this.skuId) {
      this.loadProductDetail();
    }
  },
  methods: {
    // 加载商品详情
    loadProductDetail() {
      if (!this.skuId) {
        this.$message.error('商品ID不能为空');
        return;
      }
      
      this.loading = true;
      getProductDetail(this.skuId).then(response => {
        this.productDetail = response.data;
        // 计算库存状态
        this.productDetail.inStock = this.productDetail.stock > 0;
        // 计算价格文本
        this.productDetail.priceText = `¥${this.productDetail.productSku && this.productDetail.productSku.productSellingPrice || '0.00'}`;
        // 计算折扣文本
        if (this.productDetail.productSku && this.productDetail.productSku.productPrice && this.productDetail.productSku.productSellingPrice) {
          const discount = ((this.productDetail.productSku.productPrice - this.productDetail.productSku.productSellingPrice) / this.productDetail.productSku.productPrice * 100).toFixed(1);
          this.productDetail.discountText = `省${discount}%`;
        }
        this.loading = false;
      }).catch(error => {
        console.error('加载商品详情失败:', error);
        this.$message.error('加载商品详情失败');
        this.loading = false;
      });
    },
    
    // 立即购买
    handleBuyNow() {
      if (!this.productDetail.inStock) {
        this.$message.warning('商品库存不足');
        return;
      }
      this.$message.success('跳转到购买页面');
      // TODO: 实现购买逻辑
    },
    
    // 加入购物车
    handleAddToCart() {
      if (!this.productDetail.inStock) {
        this.$message.warning('商品库存不足');
        return;
      }
      this.$message.success('已加入购物车');
      // TODO: 实现购物车逻辑
    },
    
    // 收藏商品
    handleAddToFavorites() {
      this.$message.success('已收藏商品');
      // TODO: 实现收藏逻辑
    },
    
    // 跳转到商品详情
    goToProduct(productId) {
      this.$router.push(`/product/detail/${productId}`);
    },
    
    // 格式化日期
    formatDate(date) {
      if (!date) return '';
      return new Date(date).toLocaleDateString();
    }
  }
};
</script>

<style scoped>
.product-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.product-basic-info {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.product-images {
  text-align: center;
}

.product-image {
  width: 100%;
  height: 400px;
  object-fit: cover;
  border-radius: 8px;
}

.no-image {
  height: 400px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #f5f5f5;
  border-radius: 8px;
  color: #999;
}

.no-image i {
  font-size: 48px;
  margin-bottom: 10px;
}

.product-info {
  padding-left: 20px;
}

.product-name {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 20px;
}

.price-section {
  margin-bottom: 20px;
}

.current-price {
  font-size: 28px;
  color: #e74c3c;
  font-weight: bold;
}

.original-price {
  font-size: 18px;
  color: #999;
  text-decoration: line-through;
  margin-left: 10px;
}

.discount-text {
  font-size: 14px;
  color: #67c23a;
  background: #f0f9ff;
  padding: 2px 8px;
  border-radius: 4px;
  margin-left: 10px;
}

.specifications {
  margin-bottom: 20px;
}

.specifications h3 {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
}

.spec-item {
  margin-bottom: 8px;
}

.spec-name {
  font-weight: bold;
  color: #666;
}

.spec-value {
  color: #333;
}

.stock-info {
  margin-bottom: 20px;
}

.stock-label {
  font-weight: bold;
  color: #666;
}

.stock-count {
  font-weight: bold;
}

.in-stock {
  color: #67c23a;
}

.out-of-stock {
  color: #f56c6c;
}

.stock-status {
  margin-left: 10px;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  background: #67c23a;
  color: white;
}

.stock-status.out-of-stock {
  background: #f56c6c;
}

.merchant-info {
  margin-bottom: 20px;
  padding: 15px;
  background: #f9f9f9;
  border-radius: 6px;
}

.merchant-info h3 {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
}

.merchant-details p {
  margin: 5px 0;
  color: #666;
}

.action-buttons {
  margin-top: 30px;
}

.action-buttons .el-button {
  margin-right: 15px;
  padding: 12px 30px;
}

.product-tabs {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.product-description {
  padding: 20px;
}

.product-description h3 {
  font-size: 18px;
  color: #333;
  margin-bottom: 15px;
}

.product-description p {
  line-height: 1.6;
  color: #666;
}

.specifications-detail {
  padding: 20px;
}

.no-specs, .no-reviews {
  padding: 40px;
  text-align: center;
  color: #999;
}

.reviews-section {
  padding: 20px;
}

.rating-overview {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background: #f9f9f9;
  border-radius: 6px;
}

.rating-score {
  display: flex;
  align-items: center;
  margin-right: 30px;
}

.score {
  font-size: 24px;
  font-weight: bold;
  color: #e74c3c;
  margin-right: 10px;
}

.rating-stats p {
  color: #666;
  margin: 0;
}

.reviews-list {
  border-top: 1px solid #eee;
}

.review-item {
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.review-item:last-child {
  border-bottom: none;
}

.review-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-name {
  font-weight: bold;
  color: #333;
  margin-right: 10px;
}

.review-date {
  color: #999;
  font-size: 12px;
}

.review-content {
  color: #666;
  line-height: 1.6;
}

.recommend-products {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.recommend-products h2 {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
}

.recommend-item {
  text-align: center;
  padding: 15px;
  border: 1px solid #eee;
  border-radius: 8px;
  transition: all 0.3s;
  cursor: pointer;
}

.recommend-item:hover {
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.recommend-image {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 6px;
  margin-bottom: 10px;
}

.recommend-item h4 {
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.recommend-price {
  font-size: 16px;
  color: #e74c3c;
  font-weight: bold;
  margin-bottom: 10px;
}

.recommend-actions {
  margin-top: 10px;
}

.no-data {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.no-data i {
  font-size: 48px;
  margin-bottom: 20px;
}

.no-data p {
  font-size: 16px;
  margin-bottom: 20px;
}
</style>